package com.hsck.order.kds.setup

import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.bean.SetupBean
import com.hsck.order.kds.base.isnoam.view.BaseActivity
import com.hsck.order.kds.databinding.ActivitySetupBinding
import com.hsck.order.kds.setup.adapter.SetupAdapter
import com.hsck.order.kds.setup.fragment.*
import com.hsck.order.kds.setup.fragment.cxzd.CxzdFragment
import com.hsck.order.kds.setup.fragment.dyjgl.DyjglFragment
import com.hsck.order.kds.setup.fragment.dyzd.DyzdFragment

class SetupActivity : BaseActivity<ActivitySetupBinding>() {

    private val adapter by lazy { SetupAdapter(R.layout.item_setup) }
    val listName = listOf(
        "设备管理", "厨显站点", "打印机管理", "打印站点",
        "档口设置", "菜品设置", "通用设置"
    )
    val listImg = listOf(
        R.drawable.selector_sbgl,
        R.drawable.selector_cxzd,
        R.drawable.selector_dyjgl,
        R.drawable.selector_dyzd,
        R.drawable.selector_sbgl,
        R.drawable.selector_cpsz,
        R.drawable.selector_tysz
    )
    private var currentFragment: Fragment? = null

    private var sbglFragment = SbglFragment()//设备管理
    private var cxzdFragment = CxzdFragment()//厨显站点
    private var dyjglFragment = DyjglFragment()//打印机管理
    private var dyzdFragment = DyzdFragment()//打印站点
    private var dkszFragment = DkszFragment()//档口设置
    private var cpszFragment = CpszFragment()//菜品设置
    private var tyszFragment = TyszFragment()//通用设置


    override fun inflateBinding(inflater: LayoutInflater): ActivitySetupBinding {
        return ActivitySetupBinding.inflate(inflater)
    }

    override fun initData() {
        super.initData()
        //设备管理
        //厨显站点
        //打印机管理
        //打印站点
        //档口设置
        //菜品设置
        //通用设置
        val listFragment = listOf(
            sbglFragment, cxzdFragment, dyjglFragment, dyzdFragment,
            dkszFragment, cpszFragment, tyszFragment
        )

        val listSetupBean = arrayListOf<SetupBean>()
        for (index in listName.indices) {
            val setupBean = SetupBean(listName[index], listImg[index], false)
            listSetupBean.add(setupBean)
        }
        listSetupBean.first().isSelected = true

        binding.apply {
            //退出设置
            btnSetupOutSetup.setOnClickListener {
                finish()
            }
            rvSetup.layoutManager = LinearLayoutManager(this@SetupActivity)
            rvSetup.adapter = adapter
            adapter.datas = listSetupBean
            adapter.onPositionClick = { it, index ->
                if (!it.isSelected) {
                    listSetupBean.forEach {
                        it.isSelected = false
                    }
                    listSetupBean[index].isSelected = true
                    switchFragment(listFragment[index])
                    adapter.notifyDataSetChanged()
                }
            }
        }

        switchFragment(listFragment[0])
    }


    fun switchFragment(fragment: Fragment) {
        if (currentFragment != fragment) {
            val transaction = supportFragmentManager.beginTransaction()
            (currentFragment as? UpdatableFragment)?.onFragmentInvisible()
            if (!fragment.isAdded) {
                transaction.add(R.id.frameLayout, fragment)
            }
            currentFragment?.let { transaction.hide(it) }
            transaction.show(fragment).commitAllowingStateLoss()
            currentFragment = fragment
            (fragment as? UpdatableFragment)?.onFragmentVisible()
        }
    }

    interface UpdatableFragment {
        fun onFragmentVisible()
        fun onFragmentInvisible()
    }


}